package com.atguigu.flink.day08;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.func.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2024/8/19
 * 该案例演示了处理函数
 */
public class Flink01_ProcessFunction {
    public static void main(String[] args) {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.设置并行度
        env.setParallelism(1);
        //TODO 3.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 4.对流中数据进行类型转换    String->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = socketDS.map(
                new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String lineStr) throws Exception {
                        return null;
                    }
                }
        );
        /*
        socketDS.process(
                new ProcessFunction<String, WaterSensor>() {
                    @Override
                    public void processElement(String value, ProcessFunction<String, WaterSensor>.Context ctx, Collector<WaterSensor> out) throws Exception {

                    }
                }
        )
        wsDS
                .keyBy(WaterSensor::getId)
                .process(
                        new KeyedProcessFunction<String, WaterSensor, String>() {
                            @Override
                            public void processElement(WaterSensor value, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }
                        }
                )

        wsDS
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(10)))
                .process(
                        new ProcessAllWindowFunction<WaterSensor, WaterSensor, TimeWindow>() {
                            @Override
                            public void process(ProcessAllWindowFunction<WaterSensor, WaterSensor, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<WaterSensor> out) throws Exception {

                            }
                        }
                )

        wsDS
                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                .process(
                        new ProcessWindowFunction<WaterSensor, WaterSensor, String, TimeWindow>() {
                            @Override
                            public void process(String s, ProcessWindowFunction<WaterSensor, WaterSensor, String, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<WaterSensor> out) throws Exception {

                            }
                        }
                )


        wsDS
                .connect(wsDS)
                .process(
                        new CoProcessFunction<WaterSensor, WaterSensor, String>() {
                            @Override
                            public void processElement1(WaterSensor value, CoProcessFunction<WaterSensor, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }

                            @Override
                            public void processElement2(WaterSensor value, CoProcessFunction<WaterSensor, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }
                        }
                )



        wsDS
                .keyBy(WaterSensor::getId)
                .intervalJoin(wsDS.keyBy(WaterSensor::getId))
                .between(Time.milliseconds(-5),Time.milliseconds(6))
                .process(
                        new ProcessJoinFunction<WaterSensor, WaterSensor, String>() {
                            @Override
                            public void processElement(WaterSensor left, WaterSensor right, ProcessJoinFunction<WaterSensor, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {

                            }
                        }
                )

        BroadcastStream<WaterSensor> broadcastDS
                = wsDS.broadcast(new MapStateDescriptor<String, String>("xxx", String.class, String.class));
        wsDS.connect(broadcastDS).process(

        )
         */

    }
}
